<script type="text/javascript">
	var $beritaadd_content = $("#beritaadd_content");
	$(document).ready(function (){
		var theme = 'fresh';
		$('.field_input').addClass('jqx-input');
		$('.field_input').addClass('field');
		$('.field_input').addClass('jqx-rc-all');

		var beritapost_id = $("#beritapost_id");
		var beritapost_task = $("#beritapost_task");
		var beritaadd_save = $("#beritaadd_save");
		var beritaadd_cancel = $("#beritaadd_cancel");

		var beritapost_kategori = $("#beritapost_kategori");
		var beritapost_judul = $("#beritapost_judul");
		var beritapost_tanggal = $("#beritapost_tanggal");
		var beritapost_tag = $("#beritapost_tag");
		var tinymceeditor = $(".tinymceeditor");
		var beritapost_headline = $("#beritapost_headline");
		var beritapost_publish = $("#beritapost_publish");
		var beritapost_gambar = $("#beritapost_gambar");
		var beritapost_gambarname = $("#beritapost_gambarname");

		var kategori_source = {
			datatype : "json",
			url : "<?php echo base_url();?>c_kategori/kategori_list",
			root : "results",
			datafields : [
				{ name : 'id' },
				{ name : 'kategori' }
			],
			beforeprocessing : function(data){
				kategori_source.totalrecords = data[0].TotalRows;
			}
		}
		var kategori_dataAdapter = new $.jqx.dataAdapter(kategori_source,{
			loadComplete: function (){
				<?php
				if(isset($dataDetail)){
				?>
					beritapost_kategori.find('input').val('<?php echo $dataDetail->kategori; ?>');
				<?php } ?>
			}
		});

		/* setting field type */
		beritaadd_save.jqxButton({ theme : theme, width : '70px', height : '18px' });
		beritaadd_cancel.jqxButton({ width : '70px' , height : '18px' ,theme : theme });
		beritapost_kategori.jqxComboBox({
			source : kategori_dataAdapter,
			theme : theme,
			width : 200,
			height : 25,
			autoDropDownHeight: true,
			displayMember : "kategori",
			valueMember : "id"
		});
		beritapost_tanggal.jqxDateTimeInput({
			formatString : 'dd-MM-yyyy',
			width : 150,
			height : 25,
			theme : theme,
			enableAbsoluteSelection: true
		});
		beritapost_gambar.fileupload({
			dataType: 'json',
			formData : function(form){
				return [{ name: 'kode', value: 'asdasd' }];
			},
			done: function (e, data){
				var text = "";
				$.each(data.result, function (index, file) {
					text = text + file.name;
					$("#beritapost_gambarname").val(text);
				});
			}
		});
		tinymceeditor.tinymce({
			script_url : "<?php echo base_url();?>js/tinymce/js/tinymce/tinymce.min.js",
			theme: "modern",
			plugins: [
				"advlist autolink lists link image charmap print preview hr anchor pagebreak",
				"searchreplace wordcount visualblocks visualchars code fullscreen",
				"insertdatetime media nonbreaking save table contextmenu directionality",
				"emoticons template paste textcolor jbimages"
			],
			toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image jbimages",
			toolbar2: "print preview media | forecolor backcolor emoticons",
			relative_urls: false
		});

		/* function */
		beritaadd_cancel.click(function(){
			$('#mainpanelcontent').empty();
			$('<img src="<?php echo base_url();?>images/loader.gif" style="margin:50px auto auto 390px;">').appendTo('#contents');
			$('#mainpanelcontent').load('<?php echo base_url();?>c_berita/berita_load');
		});
		beritaadd_save.click(function(){
			var id = "";
			var kategori = "";
			var judul = "";
			var tanggal = "";
			var tag = "";
			var isi = "";
			var headline = "";
			var publish = "";
			var gambar = "";
			var task = "";

			id = beritapost_id.val();
			if(beritapost_kategori.jqxComboBox('getSelectedItem') !== null){
				kategori = beritapost_kategori.jqxComboBox('getSelectedItem').value;
			}
			judul = beritapost_judul.val();
			tanggal = beritapost_tanggal.jqxDateTimeInput('getDate');
			tanggal = $.jqx.dataFormat.formatdate(tanggal, 'yyyy-MM-dd HH:mm:ss');
			tag = beritapost_tag.val();
			gambar = beritapost_gambarname.val();
			task = beritapost_task.val();

			if (beritapost_publish.prop("checked")==true) {
				publish = 1;
			}else{
				publish = 0;
			}

			if (beritapost_headline.prop("checked")==true) {
				headline = 1;
			}else{
				headline = 0;
			}

			if(task == ""){
				task = "create";
			}
			var isi = tinymceeditor.tinymce().getContent();
			
			$.ajax({
				type : 'POST',
				url : '<?php echo base_url();?>c_berita/berita_save',
				data : {
					id : id,
					kategori : kategori,
					judul : judul,
					tanggal : tanggal,
					tag : tag,
					isi : isi,
					headline : headline,
					publish : publish,
					gambar : gambar,
					task : task
				},success : function(msg){
					var result = eval(msg);
					switch(result){
						case 1:
							$('#mainpanelcontent').empty();
							$('<img src="<?php echo base_url();?>images/loader.gif" style="margin:50px auto auto 390px;">').appendTo('#contents');
							$('#mainpanelcontent').load('<?php echo base_url();?>c_berita/berita_load');
							alert("Data berhasil disimpan");
							break;
						default:
							alert("Data gagal disimpan");
							break;
					}
				}
			});
		});

		<?php
			if(isset($dataDetail)){
		?>
			beritapost_id.val('<?php echo $dataDetail->id; ?>');
			beritapost_judul.val('<?php echo $dataDetail->judul; ?>');
			var arr_tgl_ol = '<?php echo $dataDetail->created_date; ?>'.split('-');
			var set_tgl_ol = new Date(arr_tgl_ol[0], (arr_tgl_ol[1] - 1), arr_tgl_ol[2]);
			beritapost_tanggal.jqxDateTimeInput('setDate', set_tgl_ol);
			beritapost_tag.val('<?php echo $dataDetail->tag; ?>');
			tinymceeditor.html(<?php echo json_encode($dataDetail->isi_berita); ?>);
			beritapost_gambarname.val('<?php echo $dataDetail->image_berita; ?>');
			beritapost_task.val('update');
		<?php

				if($dataDetail->publish==1){
					echo "beritapost_publish.prop(\"checked\", true);";
				}else{
					echo "beritapost_publish.prop(\"checked\", false);";
				}
				if($dataDetail->headline==1){
					echo "beritapost_headline.prop(\"checked\", true);";
				}else{
					echo "beritapost_headline.prop(\"checked\", false);";
				}
			}
		?>
	});
</script>
<!-- Begin content -->
<div class="modal_header">
		<span>Berita</span>
	</div>
<div class="modal_content">
<div id="beritaadd_content">
	<form id="beritapostform" >
		<input type="hidden" id="beritapost_id">
		<input type="hidden" id="beritapost_task">
		<p>
			<label>Judul:</label><br/>
			<input type="text" id="beritapost_judul" class="field_input" style="width:500px"/>
		</p>
		<br/>
		<p>
			<label>Kategori:</label><br/>
			<div id="beritapost_kategori"></div>
		</p>
		<br/>
		<p>
			<label>Tanggal:</label><br/>
			<div id="beritapost_tanggal"></div>
		</p>
		<br/>
		<p>
			<label>Gambar:</label><br/>
			<input type="file" id="beritapost_gambar" name="userfile" style="width:300px" data-url="index.php/upload/upload_img" />
			<input type="hidden" id="beritapost_gambarname">
		</p>
		<br/>
		<p>
			<label>Tag:</label><br/>
			<input type="text" id="beritapost_tag" class="field_input" style="width:300px"/>
		</p>
		<br/>
		<p>
			<label>Isi:</label><br/>
			<textarea rows="20" cols="50" class="tinymceeditor" ></textarea>
		</p>
		<br/>
		<p>
			<input type="checkbox" class="checkbox" checked="checked" id="beritapost_headline"> <label for="cbdemo1">Headline</label>
			<input type="checkbox" class="checkbox" checked="checked" id="beritapost_publish"> <label for="cbdemo1">Publish</label>
		</p>
		<br/><br/>
		<p>
			<table align="left">
				<tr align="left">
					<td>
						<div id='beritaadd_save'>
						<img style='float: left; margin: 0px;' src='<?php echo base_url();?>images/icons/save.gif' />
						<div style='float: left; margin-left: 4px;'>Simpan</div>
					</div>
					</td>
					<td>
						<div id='beritaadd_cancel'>
						<img style='float: left; margin: 0px;' src='<?php echo base_url();?>images/icons/cross.gif' />
						<div style='float: left; margin-left: 4px;'>Cancel</div>
					</div>
					</td>
				</tr>
			</table>
		</p>
	</form>
</div>
<div class="modal_content">